package com.jty.module_user.viewmodel

import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.RegexUtils
import com.czl.lib_base.config.AppConstants
import com.jty.lib_base.base.AppManager
import com.jty.lib_base.base.BaseViewModel
import com.jty.lib_base.base.MyApplication
import com.jty.lib_base.binding.command.BindingAction
import com.jty.lib_base.binding.command.BindingCommand
import com.jty.lib_base.bus.event.SingleLiveEvent
import com.jty.lib_base.data.DataRepository
import com.jty.lib_base.event.LiveBusCenter
import com.jty.lib_base.utils.ComUtils
import com.orhanobut.logger.Logger


/**
 * author: zhouyh
 * created on: 2021/9/18 5:35 下午
 * description:主页viewmodel
 */
class UpdateViewModel(application: MyApplication, model: DataRepository) :
    BaseViewModel<DataRepository>(application, model) {

    var versionName = MutableLiveData<String>()
    var dowSize = MutableLiveData<String>()

    val uc = UIChangeEvent()

    override fun onCreate() {
        super.onCreate()
        //tvTitle.set("版本更新")
    }

    fun setParams(version: String, dowloadUrl: String, apkSize: Int) {
        Logger.i("版本升级的参数：version:${version}, dowloadUrl:${dowloadUrl}, apkSize:${apkSize}")
        versionName.postValue("发现新版本V${version}")
    }

    /**
     * UI刷新事件 用于观察者
     */
    class UIChangeEvent{
        val onClickDowLoad: SingleLiveEvent<Void> = SingleLiveEvent()
        val onFinish: SingleLiveEvent<Void> = SingleLiveEvent()
    }

    val onClickDowLoad: BindingCommand<Void> = BindingCommand(BindingAction {
       uc.onClickDowLoad.call()
    })

    override fun finish() {
        //super.finish()
        uc.onFinish.call()
    }

    fun updateDowloadSize(sum: Int, total: Int, progress: Int) {
        dowSize.postValue("${ComUtils.byte2FitMemorySize(sum.toLong())}/${ComUtils.byte2FitMemorySize(total.toLong())}")
    }


}